package com.qyi.activemq.queue.producer;

import com.qyi.activemq.queue.ActiveMQProperties;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ActiveMQPrefetchPolicy;
import org.apache.activemq.command.ActiveMQQueue;
import org.messaginghub.pooled.jms.JmsPoolConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jms.JmsPoolConnectionFactoryFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.connection.JmsTransactionManager;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.jms.core.JmsTemplate;

import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;

/**
 * @author luqi
 * @Date 2021/3/11 14:18
 * @Description TODO
 */
@Configuration
public class MqProductConfig {

    public ActiveMQConnectionFactory activeMQConnectionFactory(ActiveMQProperties properties) {
        ActiveMQConnectionFactory factory =
                new ActiveMQConnectionFactory(
                        properties.getUser(),
                        properties.getPassword(),
                        properties.getBrokerUrl());
        return factory;
    }


    @Bean(name = "producerConnection")
    JmsPoolConnectionFactory jmsConnectionFactory(ActiveMQProperties properties) {
        ActiveMQConnectionFactory connectionFactory = (activeMQConnectionFactory(properties));
        return (new JmsPoolConnectionFactoryFactory(properties.getPool())).createPooledConnectionFactory(connectionFactory);
    }

    private JmsTemplate jmsTemplate(ConnectionFactory connectionFactory, Integer priority, Integer deliveryMode, Long timeToLive) {
        JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
        //下方配置有效
        jmsTemplate.setExplicitQosEnabled(true);
        //优先级
        if (priority != null) {
            jmsTemplate.setPriority(priority);
        }
        //持久化
        if (deliveryMode != null) {
            jmsTemplate.setDeliveryMode(deliveryMode);
        }
        //过期时间 毫秒
        if (timeToLive != null) {
            jmsTemplate.setTimeToLive(timeToLive);
        }
        return jmsTemplate;
    }

    /**
     * 优先级4
     */
    @Bean(name = "templateP4")
    public JmsMessagingTemplate jmsMessagingTemplateP4(@Qualifier("producerConnection") ConnectionFactory connectionFactory) {
        return new JmsMessagingTemplate(jmsTemplate(connectionFactory, 4, DeliveryMode.PERSISTENT, null));
    }

    /**
     * 优先级5
     */
    @Bean(name = "templateP5")
    public JmsMessagingTemplate jmsMessagingTemplateP5(@Qualifier("producerConnection") ConnectionFactory connectionFactory) {
        return new JmsMessagingTemplate(jmsTemplate(connectionFactory, 5, DeliveryMode.PERSISTENT, null));
    }

    /**
     * 优先级6
     */
    @Bean(name = "templateP6")
    public JmsMessagingTemplate jmsMessagingTemplateP6(@Qualifier("producerConnection") ConnectionFactory connectionFactory) {
        return new JmsMessagingTemplate(jmsTemplate(connectionFactory, 6, DeliveryMode.PERSISTENT, null));
    }


    /**
     * 非持久化
     */
    @Bean(name = "templateNonPer")
    public JmsMessagingTemplate jmsMessagingTemplateNonPer(@Qualifier("producerConnection") ConnectionFactory connectionFactory) {
        return new JmsMessagingTemplate(jmsTemplate(connectionFactory, 4, DeliveryMode.NON_PERSISTENT, null));
    }

    /**
     * 持久化
     */
    @Bean(name = "templatePer")
    public JmsMessagingTemplate jmsMessagingTemplatePer(@Qualifier("producerConnection") ConnectionFactory connectionFactory) {
        return new JmsMessagingTemplate(jmsTemplate(connectionFactory, 4, DeliveryMode.PERSISTENT, null));
    }


    /**
     * 持久化
     */
    @Bean(name = "templateAlive")
    public JmsMessagingTemplate jmsMessagingTemplateAlive(@Qualifier("producerConnection") ConnectionFactory connectionFactory) {
        return new JmsMessagingTemplate(jmsTemplate(connectionFactory, 4, DeliveryMode.PERSISTENT, 30 * 1000L));
    }
}
